package Video
{
	import Assets.Controls.containerStyle;
	
	import SplineComponent.SplineComponentView;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mvc.Controller;
	
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.HSlider;
	import mx.controls.Label;
	import mx.controls.List;
	import mx.controls.VideoDisplay;
	import mx.effects.Move;
	import mx.events.ListEvent;
	import mx.events.SliderEvent;
	
	import util.Observable;
	
	public class VideoView extends SplineComponentView
	{
		public var containerObject:containerStyle=new containerStyle;
		public var videoContainer:TitleWindow;
		public var videoDisplayContainer:VideoDisplay;
		public var exploreHintPanel:TitleWindow;
		public var videoControlButton:Button;
		public var videoControlsContainer:TitleWindow;
		public var panelMover:Move;
		public var playButton:Button;
		public var stopButton:Button;
		public var nextButton:Button;
		public var previousButton:Button;
		public var navigationSlider:HSlider;
		public var seekBarLabel:Label;
		public var volumeBar:HSlider;
		public var volumeButton:Button;
		public var playListContainer:TitleWindow;
		public var playList:List;
		public var playListButton:Button;
		public var playListAddButton:Button;
		public var playListRemoveButton:Button;
		public var initialHeight:Number=400;
		public var initialWidth:Number=400;
		public var padding:Number=10;
		public var headerHeight:Number=30;
		public var bottomPadding:Number=20;
		public var initialPanelPositionX:Number=300;
		public var initialPanelPositionY:Number=100;
		public var backPanelEndPositionY:Number;
		public var backPanelStartPositionY:Number;
		public var playListStartPositionX:Number;
		public var playListEndPositionX:Number;
		public var controller:Object;
		private var initialWidthOfResizablePanel:Number;
		private var initialHeightOfResizablePanel:Number;
		[Bindable]
		[Embed(source="Assets/icons/videoIcons/play.JPG")]
		public var playIcon:Class;
		
		[Embed(source="Assets/icons/videoIcons/pause.JPG")]
		public var pauseIcon:Class;
		
		[Embed(source="Assets/icons/videoIcons/stop.JPG")]
		public var stopIcon:Class;
		
		[Embed(source="Assets/icons/videoIcons/volume.jpg")]
		public var volumeIcon:Class;
		
		[Embed(source="Assets/icons/videoIcons/mute.jpg")]
		public var muteIcon:Class;
		
		[Embed(source="Assets/icons/videoIcons/sidearrow.jpg")]
		public var rightArrow:Class;
		
		[Embed(source="Assets/icons/videoIcons/leftarrow.jpg")]
		public var leftArrow:Class;
		
		[Embed(source="Assets/icons/videoIcons/downarrow.jpg")]
		public var downArrow:Class;
		
		[Embed(source="Assets/icons/videoIcons/uparrow.jpg")]
		public var upArrow:Class;
		
		[Embed(source="Assets/icons/videoIcons/sliderthumb.jpg")]
		public var sliderThumb:Class;
		
		[Embed(source="Assets/icons/videoIcons/track.jpg")]
		public var trackIcon:Class;
		
		[Embed(source="Assets/icons/videoIcons/next.jpg")]
		public var nextIcon:Class;
		
		[Embed(source="Assets/icons/videoIcons/previous.jpg")]
		public var previousIcon:Class;
		
		private var lessonEditMode_bool:Boolean;
		
		private var mystage:Object=new Object;
		private var panelsArray:Array=new Array;
		
		public function VideoView(model:Observable,controller:Controller,lessonView:Object, lessonEditMode:Boolean):void
		{
			super(model,controller);
			this.controller=this.getController();
			this.mystage=lessonView;
			this.lessonEditMode_bool = lessonEditMode;
			
			initialiseVideoContainer();
			initialiseVideoControlsContainer();
			initialisePlaylistContainer();
			initialiseVideoPanel();
			initialiseExploreHintPanel();
			initialiseVideoControlButton();
			
			
			super.makePanelDraggable(this.videoContainer);
			super.makePanelResizable();
		}
		
//----------------------------------functions for resizing-----------------------------------------------------//		
		override public function setPanelVisibilty(infoObj:Object):void
		{
			this.playListContainer.visible=infoObj.panelVisibilty;
			this.videoControlsContainer.visible=infoObj.panelVisibilty;
		}
		
		override public function setNewCordinates(infoObj:Object):void
		{
			this.videoContainer.width=infoObj.finalPanelWidth;
			this.videoContainer.height=infoObj.finalPanelHeight;
			this.videoContainer.x=infoObj.finalPanelX;
			this.videoContainer.y=infoObj.finalPanelY;
			this.initialWidthOfResizablePanel=infoObj.initialWidth;
			this.initialHeightOfResizablePanel=infoObj.intialHeight;
			this.backPanelStartPositionY=infoObj.initialYOfSlidablePanel1;
			this.backPanelEndPositionY=infoObj.finalYOfSlidablePanel1;
			this.playListStartPositionX=infoObj.initialXOfSlidablePanel2;
			this.playListEndPositionX=infoObj.finalXOfSlidablePanel2;
			this.videoControlsContainer.x=infoObj.slidablePanel1X;
			this.videoControlsContainer.y=infoObj.slidablePanel1Y;
			this.playListContainer.x=infoObj.slidablePanel2X;
			this.playListContainer.y=infoObj.slidablePanel2Y;
			
			setCordinatesForAdditionalPanels();
		
		}
		
		public function setCordinatesForAdditionalPanels():void
		{	
			//------------setting for buttons for opening slidable Panels----------//
				this.videoControlButton.x=this.videoContainer.width-2*(padding)-20;
				this.playListButton.x=this.videoContainer.width-2*(padding)-20;
			
			//--------------------------------------------------------------------//
			
			//-----------------setting for video controls container---------------//
			
			this.videoControlsContainer.x=this.videoContainer.x;
			this.videoControlsContainer.width=this.videoContainer.width;
			this.videoControlsContainer.y=this.videoContainer.y+this.videoContainer.height-100;
			this.videoControlsContainer.height=100;
			//--------------------------------------------------------------------//
			
			//-----------------setting for playList container---------------------//
			this.playListContainer.height=this.videoContainer.height;
			this.playListContainer.width=200;
			this.playListContainer.x=this.videoContainer.x+this.videoContainer.width-200;
			this.playListContainer.y=this.videoContainer.y
			this.playListAddButton.y=this.playListContainer.height-70;
			this.playListRemoveButton.y=this.playListContainer.height-70;
			this.playList.height=this.playListContainer.height-70;
			//--------------------------------------------------------------------//
			
			var ratioX:Number = (this.videoContainer.width - initialWidthOfResizablePanel)/initialWidthOfResizablePanel;
			var ratioY:Number = (this.videoContainer.height - initialHeightOfResizablePanel)/initialHeightOfResizablePanel;
			
			
		}
		
		
//-------------------------------------------------------------------------------------------------------------//		
		public function initialiseVideoContainer():void
		{
			
			videoContainer=new TitleWindow;
			videoContainer=containerObject.setPanel(initialPanelPositionX,initialPanelPositionY,initialWidth,initialHeight,true,1,"Video","absolute",padding,padding,headerHeight,bottomPadding);
			panelsArray.push(this.videoContainer);
		}
		
		
		public function initialiseVideoControlsContainer():void
		{
			videoControlsContainer=new TitleWindow;
			videoControlsContainer=containerObject.setPanel(videoContainer.x,initialPanelPositionY+videoContainer.height-100,videoContainer.width,100,true,1,"VIDEO CONTROLS","absolute",10,10,20,20);
			
			backPanelStartPositionY=videoControlsContainer.y;
			backPanelEndPositionY=videoControlsContainer.y+90;
			this.panelsArray.push(this.videoControlsContainer);
		}
			
		public function initialisePlaylistContainer():void
		{
				playListContainer=new TitleWindow;
				playList=new List;
				
				playListContainer=containerObject.setPanel(initialPanelPositionX+videoContainer.width-200,videoContainer.y,200,videoContainer.height,true,1,"PLAYLIST","absolute",10,10,30,20);
				
				playList.x=0;
				playList.y=0;
				playList.width=180;
				playList.height=playListContainer.height-70;
				playList.doubleClickEnabled=true;
			
				playList.addEventListener(ListEvent.ITEM_CLICK,evtListner_playListClicked);
				playList.addEventListener(ListEvent.ITEM_DOUBLE_CLICK,evtListner_playListDoubleClicked);
				playListStartPositionX=playListContainer.x;
				playListEndPositionX=playListContainer.x+190;
				
				
				playListAddButton=new Button;
				playListAddButton=containerObject.setButton(0,playList.height,0.5*playList.width,18,true,"ADD TO PLAYLIST",null,"+",0);
				playListAddButton.addEventListener(MouseEvent.CLICK,evtListner_openPlayList);
				playListContainer.addChild(playListAddButton);
				
				playListRemoveButton=new Button;
				playListRemoveButton=containerObject.setButton(playListAddButton.width,playList.height,0.5*playList.width,18,true,"REMOVE FROM PLAYLIST",null,"-",0);
				playListRemoveButton.addEventListener(MouseEvent.CLICK,evtListner_removeFromPlayList);
				playListContainer.addChild(playListRemoveButton);
				
				this.mystage.addChildToStage(playListContainer);
				this.mystage.addChildToStage(videoControlsContainer);
				this.mystage.addChildToStage(videoContainer);
				playListContainer.addChild(playList);
				
		}
		
		public function initialiseVideoPanel():void
		{
				videoDisplayContainer=new VideoDisplay;
				videoDisplayContainer.x=0;
				videoDisplayContainer.y=0;
				videoDisplayContainer.height=initialHeight-100;
				videoDisplayContainer.width=videoContainer.width-(2*padding);
				videoContainer.addChild(videoDisplayContainer);
				videoDisplayContainer.autoPlay=false;
				
		}
		
		public function initialiseExploreHintPanel():void
		{
			exploreHintPanel=new TitleWindow;
			exploreHintPanel=containerObject.setPanel(0,videoDisplayContainer.height,videoContainer.width-(2*padding),48,true,1,null,"absolute",0,0,0,0)
			videoContainer.addChild(exploreHintPanel);
		}
		
		public function initialiseVideoControlButton():void
		{
			videoControlButton=new Button;
			videoControlButton=containerObject.setButton(exploreHintPanel.width-20,28,10,10,true,"OPEN VIDEO CONTROL PANEL",null,null,10);
			videoControlButton.setStyle("icon",downArrow);
			exploreHintPanel.addChild(videoControlButton);
			videoControlButton.addEventListener(MouseEvent.CLICK,evtListner_openVideoControlsPanel);
			
			playListButton=new Button;
			playListButton=containerObject.setButton(exploreHintPanel.width-20,6,10,10,true,"OPEN PLAYLIST",null,null,10);
			playListButton.setStyle("icon",rightArrow);
			exploreHintPanel.addChild(playListButton);
			playListButton.addEventListener(MouseEvent.CLICK,evtListner_openPlayListPanel);
			
			//initialising play pause and stop button
			
			playButton=new Button;
			playButton=containerObject.setButton(5,5,25,25,true,"PLAY",null,null,25);
			playButton.addEventListener(MouseEvent.CLICK,evtListner_play);
			playButton.setStyle("icon",playIcon);
			videoControlsContainer.addChild(playButton);
			
			stopButton=new Button;
			stopButton=containerObject.setButton(40,5,25,25,true,"STOP",null,null,25);
			stopButton.addEventListener(MouseEvent.CLICK,evtListner_stop);
			stopButton.setStyle("icon",stopIcon);
			videoControlsContainer.addChild(stopButton);
			
			nextButton=new Button;
			nextButton=containerObject.setButton(75,5,25,25,true,"NEXT",null,null,25);
			nextButton.addEventListener(MouseEvent.CLICK,evtListner_next);
			nextButton.setStyle("icon",nextIcon);
			videoControlsContainer.addChild(nextButton);
			
			previousButton=new Button;
			previousButton=containerObject.setButton(110,5,25,25,true,"PREVIOUS",null,null,25);
			previousButton.addEventListener(MouseEvent.CLICK,evtListner_previous);
			previousButton.setStyle("icon",previousIcon);
			videoControlsContainer.addChild(previousButton);
			
			seekBarLabel=new Label;
			seekBarLabel.x=310;
			seekBarLabel.y=40;
			seekBarLabel.width=70;
			seekBarLabel.text="0.00/0.0";
			videoControlsContainer.addChild(seekBarLabel);
			
			navigationSlider=new HSlider;
			navigationSlider.width=videoDisplayContainer.width-90;
			navigationSlider.x=0;
			navigationSlider.y=35;
			
			//navigationSlider.setStyle("thumbSkin",sliderThumb);
	 		//navigationSlider.setStyle("showTrackHighlight",true);
			navigationSlider.setStyle("trackSkin",trackIcon);
			navigationSlider.setStyle("invertThumbDirection",true);
			videoControlsContainer.addChild(navigationSlider);
			
			volumeButton=new Button;
			volumeButton=containerObject.setButton(200,5,25,25,true,"VOLUME",null,null,25);
			volumeButton.addEventListener(MouseEvent.CLICK,evtListner_onVolumeClicked);
			volumeButton.setStyle("icon",volumeIcon);
			videoControlsContainer.addChild(volumeButton);
			
			
			volumeBar=new HSlider;
			volumeBar.x=230;
			volumeBar.y=5;
			volumeBar.width=150;
			volumeBar.maximum=1;
			volumeBar.value=0.3;
			volumeBar.setStyle("trackSkin",trackIcon);
			videoDisplayContainer.volume=volumeBar.value;
			videoControlsContainer.addChild(volumeBar);
			volumeBar.addEventListener(SliderEvent.CHANGE,evtListner_volumeControl);
		}
		
		/*----------event listners fir sliding panel------------------------*/
		public function evtListner_openVideoControlsPanel(evt:MouseEvent):void
		{
			
			panelMover=new Move;
			panelMover.target=videoControlsContainer;
			
			if(videoControlsContainer.y==backPanelStartPositionY)
			{
				videoControlButton.setStyle("icon",upArrow);
				panelMover.yFrom=backPanelStartPositionY;
				panelMover.yTo=backPanelEndPositionY;
				
			}
			else
			{
				videoControlButton.setStyle("icon",downArrow);
				panelMover.yFrom=backPanelEndPositionY;
				panelMover.yTo=backPanelStartPositionY;
				
			}
			panelMover.play();
		}
		
		
		public function evtListner_openPlayListPanel(evt:MouseEvent):void
		{
			panelMover=new Move;
			panelMover.target=playListContainer;
			
			if(playListContainer.x==playListStartPositionX)
			{
				
				playListButton.setStyle("icon",leftArrow);
				panelMover.xFrom=playListStartPositionX;
				panelMover.xTo=playListEndPositionX;
				playListContainer.title="PLAYLIST";
			}
			else
			{
				playListButton.setStyle("icon",rightArrow);
				panelMover.xFrom=playListEndPositionX;
				panelMover.xTo=playListStartPositionX;
				playListContainer.title=null;
				
			}
			
			panelMover.play();
		}
		
		/*---------------------------------------------------------------------------*/
		
		
		public function evtListner_play(evt:Event):void
		{
			
			controller.playVideo();
		}
		
		public function evtListner_next(evt:Event):void
		{
			controller.playNextVideo();
		}
		
		public function evtListner_previous(evt:Event):void
		{
			controller.playPreviousVideo();
		}
		
		
		public function evtListner_stop(evt:Event):void
		{
			controller.stopVideo();
		}
		
		public function evtListner_onVolumeClicked(evt:Event):void
		{
			if(volumeBar.value!=0)
			{
				volumeButton.setStyle("icon",muteIcon);
				volumeBar.value=0;
				videoDisplayContainer.volume=volumeBar.value;
			}
			else
			{
				volumeButton.setStyle("icon",volumeIcon);
				volumeBar.value=0.5;
				videoDisplayContainer.volume=volumeBar.value;
			}
		}
		public function evtListner_volumeControl(evt:SliderEvent):void
		{
			videoDisplayContainer.volume=volumeBar.value;
		}
		
		
		public function evtListner_openPlayList(evt:MouseEvent):void
		{
			controller.openFileList();
		}
		
		public function evtListner_removeFromPlayList(evt:MouseEvent):void
		{
			controller.removeVideoFromList();
		}
		
		public function evtListner_playListClicked(evt:Event):void
		{
			controller.onPlayListClicked();
		}
		public function evtListner_playListDoubleClicked(evt:Event):void
		{
			controller.onPlayListDoubleClicked();
		}
		
		//-----------------------------------------------------communication function-----------------------------------------------//
		override public function update(model:Observable, infoObj_object:Object):void
		{
			var message:String=infoObj_object.message.toString();
			
			switch(message)
			{
				case "mouseUpEvent":
					trace("VideoView------->event:mouseup  infoObj.event :"+infoObj_object.event);
					super.recieveMouseUpEvent(infoObj_object.event);
					break;
					
				case "close":
					trace("removing video from stage");
					this.videoDisplayContainer.close();
					this.videoContainer.removeChild(this.videoDisplayContainer);
					this.mystage.removeChildFromStage(this.videoContainer);
					this.mystage.removeChildFromStage(this.playListContainer);
					this.mystage.removeChildFromStage(this.videoControlsContainer);
					trace("video removed from stage");
					break;
			}
		}
	}
}